from django.forms.widgets import RadioChoiceInput, RadioFieldRenderer, RendererMixin, Select
from django.forms.utils import flatatt
from django.utils.html import format_html

import logging
logger = logging.getLogger('django')

class ThumbnailRadioChoiceInput(RadioChoiceInput):
    def tag(self, attrs=None):
        img_attrs = {
                    'class':    'thumbnail-radiobutton',
                    'src':      self.choice_label,
                    'alt':      self.choice_value,
                }
        thumbnail = format_html('<img {} />', flatatt(img_attrs))
        attrs['class'] = attrs.get('class', '') + ' img-radio-button'
        self.choice_label = ''
        return super().tag(attrs) + thumbnail

class ThumbnailRadioFieldRenderer(RadioFieldRenderer):
    choice_input_class = ThumbnailRadioChoiceInput

class ThumbnailRadioSelect(RendererMixin, Select):
    renderer = ThumbnailRadioFieldRenderer
